<?php

	class EditReportageCommand implements ICommand
	{
		public function execute($event)
		{
			$name 		= $event->data['nom'];
			$urlname 	= EbUrlRoutingToolkit::formatToUrl($name);
			$model 		= EbModel::getModel('VarPeople','Reportage');
			$buffer 	= $model->selectByUrlName($name);
			$newYN 		= !isset($event->data['rep_id']);
						
			if ($newYN && count($buffer) > 0)
			{
				return new Event('redirect','/admin-reportage');
			}
			
			$buffer = new StdClass();
			
			$buffer->nom 		= $name;
			$buffer->urlname 	= $urlname;
			$buffer->description = $event->data['description'];
			$buffer->ville 		= $event->data['ville'];
			$buffer->date 		= $event->data['date'];
						
			if ($newYN)
			{
				$mediafolder_id = EbModel::getModel('Media','MediaFolder')->insert((object) array('name' => $buffer->nom,'pr_id' => 8));
				
				$buffer->mediafolder_id = $mediafolder_id;
				
				$model->insert($buffer);
			}
			else
			{
				$buffer->rep_id = $event->data['rep_id'];
				
				$model->update($buffer);
			}
			
			return new Event('redirect','/admin-reportage');
				
		}		
	}